﻿using Events.Dto;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.EventBus;

namespace RedPacket.Events
{
    /// <summary>
    /// 快递事件
    /// </summary>
    public class Express : IEventSubscriber, ISingleton
    {
        private readonly ISqlSugarClient db;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="db"></param>
        public Express(ISqlSugarClient db)
        {
            this.db = db;
        }

        /// <summary>
        /// 发送快递后调用，设置快递单号和快递状态
        /// </summary>
        /// <returns></returns>
        [EventSubscribe("sendExpress")]
        public async Task Send(EventHandlerContext e)
        {
            var data = (sendExpress)e.Source.Payload;

            var express = await db.Queryable<Entity.Express>().FirstAsync(x => x.Id == data.ExpressId);

            express.ExpressState = Entity.ExpressState.配送中;

            express.ExpressCode = data.Code ?? "错误单号";

            await db.Updateable(express).ExecuteCommandHasChangeAsync();
        }
    }
}
